<?php
// 连接数据库
function my_connect($host, $username, $password, $char, $dbname)
{
    // 创建连接
    $conn = mysqli_connect($host, $username, $password);
    // 检测连接
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_errno().":".mysqli_connect_error());
    }
    mysqli_set_charset($conn, "utf8");
    mysqli_query($conn,"set names 'utf8'");//读库
    mysqli_query($conn,"set names 'utf8'");//写库
    // 选择数据库
    mysqli_select_db($conn, $dbname) or die("打开数据库:".$dbname." Error:".mysqli_connect_errno().":".mysqli_connect_error());
    return $conn;
}

// 关闭数据库连接
function my_close($conn=null)
{
    mysqli_close($conn);
}

// 执行sql语句
function my_exec($conn, $sql)
{
    if (!mysqli_query($conn, $sql)){
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
}
?>